package hibernate;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.Query;
import photogallery.Album;
import photogallery.HibernateUtil;
import photogallery.Image;

/**
 *
 * @author ThuanPQ
 */
public class AlbumDAO {

    private Session session;

    public AlbumDAO() {
        session = HibernateUtil.getSessionFactory().openSession();
    }

    public Album[] findByAlbumName(String albumName) {
        try {
            session.getTransaction().begin();

            String sql = "from Album where name like ?";
            Query query = session.createQuery(sql);
            query.setString(0, "%" + albumName + "%");
            List resultList = query.list();

            Album[] albums = new Album[resultList.size()];
            resultList.toArray(albums);
            session.getTransaction().commit();
            return albums;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return null;
    }

    public Album findByAlbumID(Integer albumID) {
        try {
            session.getTransaction().begin();

            String sql = "from Album where album_id=?";
            Query query = session.createQuery(sql);
            query.setString(0, String.valueOf(albumID));
            List resultList = query.list();

            Album[] albums = new Album[resultList.size()];
            resultList.toArray(albums);
            session.getTransaction().commit();
            return albums[0];
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return null;
    }

    public String getAlbumCoverUrl(Integer albumId) {
        try {
            session.getTransaction().begin();
            String sql = "from Image where is_cover=1 and album_id =?";
            Query query = session.createQuery(sql);
            query.setString(0, String.valueOf(albumId));
            List resultList = query.list();
            if (resultList.size() > 0) {
                Image[] image = new Image[resultList.size()];
                resultList.toArray(image);
                session.getTransaction().commit();
                return image[0].getUrl();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public Album[] getAllAlbums() {
        try {
            session.getTransaction().begin();
            String sql = "from Album";
            Query query = session.createQuery(sql);
            List resultList = query.list();
            Album[] albums = new Album[resultList.size()];
            resultList.toArray(albums);
            session.getTransaction().commit();
            return albums;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public Album createAlbum(String _name) {
        try {
            Date date = new Date();
            session.getTransaction().begin();
            Album album = new Album(_name, date);
            session.save(album);
            session.flush();
            session.getTransaction().commit();
            return album;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return null;
    }

    public void editAlbum(Integer albumId, String newName) {
        try {
            session.getTransaction().begin();
            Album album = (Album) session.get(Album.class, albumId);
            album.setName(newName);
            session.update(album);
            session.flush();
            session.getTransaction().commit();
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
    }

    public void deleteAlbum(Integer albumId) {
        try {
            session.getTransaction().begin();
            Album album = (Album) session.get(Album.class, albumId);
            session.delete(album);
            session.flush();
            session.getTransaction().commit();
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
    }

    public void insertImages(Integer _albumId, Integer _imageId) {
        try {
            session.getTransaction().begin();
            Album album = (Album) session.get(Album.class, _albumId);
            Image img = (Image) session.get(Image.class, _imageId);
            album.getImages().add(img);
            session.save(img);
            session.flush();
            session.getTransaction().commit();
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
    }

    public void autoSelectCover(Integer albumId) {
        try {
            session.getTransaction().begin();
            Album album = (Album) session.get(Album.class, albumId);
            Image[] images = fromSetToArray(album);
            if(images.length > 0) images[0].setIsCover(true);
            session.update(images[0]);
            session.flush();
            session.getTransaction().commit();
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
    }

    private Image[] fromSetToArray(Album album) {
        Image[] images = new Image[album.getImages().size()];
        Iterator<Image> it = album.getImages().iterator();
        int i = 0;
        while (it.hasNext()) {
            images[i] = it.next();
            i++;
        }
        return images;
    }
}
